from datetime import datetime
from sqlalchemy import DECIMAL, DateTime, func
from app import db
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin


class User(db.Model, UserMixin):
    __tablename__ = 'users'
    __table_args__ = {'mysql_comment': '用户表'}
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(20), unique=True, nullable=False, comment="用户名")
    email = db.Column(db.String(120), unique=True, nullable=False, comment="邮箱")
    password_hash = db.Column(db.String(255), nullable=False, comment="hash密码")
    created_at = db.Column(DateTime, server_default=func.now(), comment="创建时间")
    updated_at = db.Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")
    is_admin = db.Column(db.Boolean, default=False)

    def __init__(self, username, email, password, is_admin=False):
        self.username = username
        self.email = email
        self.set_password(password)
        self.created_at = func.now()
        self.updated_at = func.now()
        self.is_admin = is_admin

    @staticmethod
    def init_admin():
        # 检查是否已存在管理员用户
        admin = User.query.filter_by(username='admin').first()
        if admin is None:
            admin = User(
                username='admin',
                email='admin@admin.com',
                password='123456',
                is_admin=True
            )
            db.session.add(admin)
            db.session.commit()
            return True
        return False

    def __repr__(self):
        return '<User %r>' % self.username

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)


class Movies(db.Model):
    __tablename__ = 'movies'
    __table_args__ = {'mysql_comment': '电影信息表'}
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), nullable=False, comment="电影名")
    director = db.Column(db.String(100), nullable=False, comment="导演")
    country = db.Column(db.String(100), nullable=False, comment="上映国家")
    years = db.Column(db.String(100), nullable=False, comment="上映年月")
    leader = db.Column(db.Text, nullable=False, comment="主演")
    rate_int = db.Column(db.Integer, nullable=False, comment="电影评分 整数")
    rate = db.Column(DECIMAL(19, 2), nullable=False, comment="电影评分 小数")
    intro = db.Column(db.Text, nullable=False, comment="电影简介")
    views = db.Column(db.Integer, nullable=False, default=0, comment="浏览数量")
    image_link = db.Column(db.String(255), nullable=False, comment="图片地址")
    created_at = db.Column(DateTime, server_default=func.now(), comment="创建时间")
    updated_at = db.Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")

    def __repr__(self):
        return '<Movie %r>' % self.name

    def __init__(self, name, director, country, years, leader, rate, intro, image_link):
        self.name = name
        self.director = director
        self.country = country
        self.years = years
        self.leader = leader
        self.rate_int = int(rate)
        self.rate = rate
        self.intro = intro
        self.image_link = image_link


class Tags(db.Model):
    __tablename__ = 'tags'
    __table_args__ = {'mysql_comment': '电影标签'}
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), unique=True, nullable=False, comment="标签名")

    def __repr__(self):
        return '<Tag %r>' % self.name

    def __int__(self, name):
        self.name = name


class MovieTags(db.Model):
    __tablename__ = 'movie_tags'
    __table_args__ = {'mysql_comment': '电影关联标签'}
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    movie_id = db.Column(db.Integer, db.ForeignKey('movies.id'), nullable=False)
    tag_id = db.Column(db.Integer, db.ForeignKey('tags.id'), nullable=False)
    created_at = db.Column(DateTime, server_default=func.now(), comment="创建时间")
    updated_at = db.Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")


class UserTags(db.Model):
    __tablename__ = 'user_tags'
    __table_args__ = {'mysql_comment': '用户感兴趣标签'}
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    tag_id = db.Column(db.Integer, db.ForeignKey('tags.id'), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    created_at = db.Column(DateTime, server_default=func.now(), comment="创建时间")
    updated_at = db.Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")

    def __repr__(self):
        return '<UserTag %r>' % self.user_id

    def __init__(self, user_id, tag_id):
        self.user_id = user_id
        self.tag_id = tag_id


class Country(db.Model):
    __tablename__ = 'country'
    __table_args__ = {'mysql_comment': '国家/国家特别行政区表'}
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name_en = db.Column(db.String(100), nullable=False, comment="英文全名")
    name_zh = db.Column(db.String(100), nullable=True, comment="中文全名")
    name_international_abbr = db.Column(db.String(100), nullable=False, comment="名缩写")
    country_phone_code = db.Column(db.Integer, nullable=False, comment="电话代码")
    zone = db.Column(db.String(100), nullable=False, comment="所属洲")
    zone_alias = db.Column(db.String(100), nullable=True, comment="所属分区 如: 中东")
    lat = db.Column(db.String(100), nullable=True, comment="所处纬度")
    lon = db.Column(db.String(100), nullable=True, comment="所处经度")

    def __repr__(self):
        return '<Country %r>' % self.name_zh
